﻿using System;
using System.Windows;
using eProject.Model;
using eProject.ViewModel;

namespace eProject.View
{
    /// <summary>
    /// Interaction logic for CreateActivityDialog.xaml
    /// </summary>
    public partial class CreateActivityDialog
    {      

        private readonly CommonManager _manager;

        public TaskViewModel Task
        {
            get
            {
                return tasksComboBox.SelectedItem as TaskViewModel;
            }
            set
            {
                tasksComboBox.SelectedItem = value;
            }
        }

        public EmployeeViewModel Employee {
            get
            {
                return employeesComboBox.SelectedItem as EmployeeViewModel;
            }
            set
            {
                employeesComboBox.SelectedItem = value;
            }
        }
        public Period Period { get; private set; }

        public string Description { get; set; }

        public DateTime Begin
        {
            get
            {
                return fromDateTime.Value.Date;
            }
            set
            {
                fromDateTime.Value = value.Date;
            }
        }

        public DateTime End
        {
            get
            {
                return toDateTime.Value.Date;
            }
            set
            {
                toDateTime.Value = value.Date;
            }
        }
        public int Priority { get; set; }

        public CreateActivityDialog(CommonManager manager)
        {
            if (manager == null) throw new ArgumentNullException("manager");
            _manager = manager;
            InitializeComponent();
            DataContext = this;
            Title = Properties.Resources.CreateActivityDialogHeader;
            tasksComboBox.DataContext = _manager.Tasks;
            employeesComboBox.DataContext = _manager.Employees;
            Priority = 5;
        }

        public CreateActivityDialog(CommonManager manager, ActivityViewModel activity) : this(manager)
        {
            tasksComboBox.IsEnabled = false;
            employeesComboBox.IsEnabled = false;
            Title = Properties.Resources.EditActivityDialogHeader;
            Task = activity.Task;
            Employee = activity.Employee;
            Description = activity.Description;
            Begin = activity.Period.Begin;
            End = activity.Period.End;
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            Task = tasksComboBox.SelectedItem as TaskViewModel;
            Employee = employeesComboBox.SelectedItem as EmployeeViewModel;
            Period = new Period(Begin, End);
            DialogResult = true;

        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            DialogResult = false;
        }

        private void fromDateTime_ValueChanged(object sender, EventArgs e)
        {
            var from = fromDateTime.Value;
            var to = toDateTime.Value;

            if (to <= from)
            {
                toDateTime.Value = from.AddDays(1);
            }
        }               
    }
}
